new Vue({
    el: "#app",
    data: {
        fullscreenLoading: false,
        screenLoading: false,
        dialogDeleteVisible: false,
        dialogNewVisible: false,
        dialogUpdateVisible: false,
        recommendData: [],
        newRecommend:{},
        updateRecommend:{},
        allProduct:[],
        id: null,
    },
    mounted: function () {
        var _this = this;
        _this.loadRecommendList();
    },
    methods: {
        trim: function (str) {
            if (isNaN(str)) {
                return str.replace(/(^\s*)|(\s*$)/g, "");
            } else {
                return str
            }
        },
        loadRecommendList: function () {
            var _this = this;
            axios.get(request_prefix + "/cash/recommend/list")
                .then(function (res) {
                    if (res.data.code === 0) {
                        _this.recommendData = res.data.data;
                    } else {
                        _this.$message.error(res.data.message);
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程发生错误");
                })
        },
        filterField:function (field) {
          var result = "";
          switch(field) {
              case 1 : result = "推荐位置1";break;
              case 2 : result = "推荐位置2";break;
              case 3 : result = "推荐位置3";break;
              case 4 : result = "推荐位置4";break;
              default:
                  result = "";
                  break;
          }
          return result;
        },
        toNewDialog: function () {
            var _this = this;
            axios.get(request_prefix + "/cash/product/all")
                .then(function (res) {
                    if(res.data.code === 0) {
                        _this.allProduct = res.data.data;
                        _this.dialogNewVisible = true;
                    } else {
                        _this.$message.error(res.data.message);
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程中发生错误");
                })
        },
        confirmCreate: function () {
            var _this = this;
            if (_this.newRecommend.name == null || _this.newRecommend.name == '') {
                _this.$message.warning("推荐标题名称不能为空");
                return;
            }
            if (_this.newRecommend.field == null || _this.newRecommend.field == '') {
                _this.$message.warning("位置不能为空");
                return;
            }
            if (_this.newRecommend.productId == null || _this.newRecommend.productId == '') {
                _this.$message.warning("产品不能为空");
                return;
            }
            axios.post(request_prefix + "/cash/recommend/add", _this.newRecommend)
                .then(function (res) {
                    if (res.data.code === 0) {
                        _this.loadRecommendList();
                        _this.dialogNewVisible = false;
                    } else {
                        _this.$message.error(res.data.message);
                        _this.dialogNewVisible = false;
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程中发生错误");
                })
        },
        toEditDialog: function (id) {
            var _this = this;
            axios.get(request_prefix + "/cash/recommend/detail", {
                params: {
                    id: id
                }
            })
                .then(function (res) {
                    if (res.data.code == 0) {
                        _this.updateRecommend = res.data.data;
                        _this.updateRecommend.field = _this.updateRecommend.field + "";
                        axios.get(request_prefix + "/cash/product/all")
                            .then(function (res) {
                                if(res.data.code === 0) {
                                    _this.allProduct = res.data.data;
                                    _this.dialogUpdateVisible = true;
                                } else {
                                    _this.$message.error(res.data.message);
                                }
                            })
                            .catch(function (res) {
                                _this.$message.error("请求过程中发生错误");
                            })

                    } else {
                        _this.$message.error(res.data.message);
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程发生错误");
                })
        },
        confirmUpdate: function () {
            var _this = this;
            if (_this.updateRecommend.name == null || _this.updateRecommend.name == '') {
                _this.$message.warning("推荐标题名称不能为空");
                return;
            }
            if (_this.updateRecommend.field == null || _this.updateRecommend.field == '') {
                _this.$message.warning("位置不能为空");
                return;
            }
            if (_this.updateRecommend.productId == null || _this.updateRecommend.productId == '') {
                _this.$message.warning("产品不能为空");
                return;
            }
            axios.post(request_prefix + "/cash/recommend/update",_this.updateRecommend)
                .then(function (res) {
                    if (res.data.code === 0) {
                        _this.loadRecommendList();
                        _this.dialogUpdateVisible = false;
                    } else {
                        _this.$message.error(res.data.message);
                        _this.dialogUpdateVisible = false;
                    }
                })
                .catch(function (res) {
                    _this.$messgage.error("请求过程中发生错误");
                })
        },
        deleteRecommend: function (id) {
            var _this = this;
            _this.id = id;
            _this.dialogDeleteVisible = true;
        },
        confirmDelete: function () {
            var _this = this;
            var params = new URLSearchParams();
            params.append("id",_this.id);
            axios.post(request_prefix + "/cash/recommend/delete",params)
                .then(function (res) {
                    if(res.data.code === 0) {
                        _this.loadRecommendList();
                        _this.dialogDeleteVisible = false;
                    } else {
                        _this.$message.error(res.data.message);
                        _this.dialogDeleteVisible = false;
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程发生错误");
                })
        }
    }
});